package cn.rht.study.controller;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.rht.study.controller.ex.FileIsEmptyException;
import cn.rht.study.controller.ex.FileOutException;
import cn.rht.study.controller.ex.FileStateException;
import cn.rht.study.controller.ex.FileTypeNotSupportException;
import cn.rht.study.controller.ex.FileUploadException;
import cn.rht.study.controller.ex.FileUploadIOException;
import cn.rht.study.service.ex.EmptyListException;
import cn.rht.study.service.ex.OtherException;
import cn.rht.study.service.ex.PasswordNotMatchException;
import cn.rht.study.service.ex.ServiceException;
import cn.rht.study.service.ex.UserNameIExitsException;
import cn.rht.study.service.ex.UsernameNotFoundException;
import cn.rht.study.util.JsonResult;

public class BaseController {
	
	public static final Integer SUCCESS = 200;
	protected static Logger log = LoggerFactory.getLogger(BaseController.class);
	
	@ExceptionHandler({ServiceException.class,FileUploadException.class})
	@ResponseBody
	public JsonResult<Void> handlerException(Throwable e){
		log.error(e.getMessage());
		JsonResult<Void> jr = new JsonResult<>(e);
		jr.setMessage(e.getMessage());
		if(e instanceof UsernameNotFoundException) {
			jr.setCode(401);
		}else if(e instanceof PasswordNotMatchException) {
			jr.setCode(402);
		}else if(e instanceof UserNameIExitsException) {
			jr.setCode(403);
		}else if(e instanceof OtherException) {
			jr.setCode(405);
		}else if(e instanceof FileOutException) {
			jr.setCode(501);
		}else if(e instanceof EmptyListException) {
			jr.setCode(502);
		}else if(e instanceof FileIsEmptyException) {
			jr.setCode(503);
		}else if(e instanceof FileTypeNotSupportException) {
			jr.setCode(504);
		}else if(e instanceof FileStateException) {
			jr.setCode(505);
		}else if(e instanceof FileUploadIOException) {
			jr.setCode(506);
		}
		return jr;
	}
}
